/*
 * MessageModel.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Message;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.MessageReadRequest;
import com.yidiandao.business.data.request.MessageRequest;
import com.yidiandao.business.data.response.MessageResponse;
import com.yidiandao.business.data.response.UnReadMessageResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.MessageTimeListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.NormalAssetMessageListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.NormalFavorableMessageListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.NormalLogisticsMessageListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.NormalSystemMessageListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息中心Model
 */
public class MessageModel implements IMessageModel {

    private NetBiz mNetBiz = new NetBiz();

    private int mPage;

    private List<DisplayListItem> mDisplayListItems = new ArrayList<>();

    private List<Object> mObjects = new ArrayList<>();

    @Override
    public void getUnMessageCountFromServer(String tag, final OnGetUnReadMessageCountListener listener) {
        BaseRequest request = new BaseRequest();
        request.setUrl(Urls.MESSAGE_CENTER);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, UnReadMessageResponse.class, new NetListener<UnReadMessageResponse>() {
            @Override
            public void onSuccess(UnReadMessageResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData().getUnReadMessage());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void loadAssetMessages(final boolean isRefreshing, String tag, final OnGetFromNetListener listener) {

        if (isRefreshing) {
            mPage = 1;
        }

        final MessageRequest request = new MessageRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.MESSAGE_NEWS_LIST);
        request.setPage(mPage);
        request.setType(MessageRequest.Type.ASSET);
        request.setPageSize(Common.PAGER_SIZE);
        mNetBiz.sendGetRequest(request, MessageResponse.class, new NetListener<MessageResponse>() {
            @Override
            public void onSuccess(MessageResponse response) {
                mPage++;
                if (isRefreshing) {
                    mDisplayListItems.clear();
                }

                List<Message> assetMessageList = response.getData().getNewsList();
                List<DisplayListItem> assetListItems = Functional.serverToClient(assetMessageList, new Functional.Processor<Message, DisplayListItem>() {
                    @Override
                    public DisplayListItem processToClient(Message assetMessage) {
                        return new NormalAssetMessageListItem(assetMessage);
                    }
                });
                mDisplayListItems.addAll(assetListItems);

                if (listener != null) {
                    listener.onSuccess(mDisplayListItems, assetMessageList.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void loadLogisticsMessages(final boolean isRefreshing, String tag, final OnGetFromNetListener listener) {
        if (isRefreshing) {
            mPage = 1;
        }

        MessageRequest request = new MessageRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.MESSAGE_NEWS_LIST);
        request.setPage(mPage);
        request.setType(MessageRequest.Type.ORDER);
        request.setPageSize(Common.PAGER_SIZE);
        mNetBiz.sendGetRequest(request, MessageResponse.class, new NetListener<MessageResponse>() {
            @Override
            public void onSuccess(MessageResponse response) {
                mPage++;
                if (isRefreshing) {
                    mDisplayListItems.clear();
                }

                List<Message> assetMessageList = response.getData().getNewsList();
                List<DisplayListItem> assetListItems = Functional.serverToClient(assetMessageList, new Functional.Processor<Message, DisplayListItem>() {
                    @Override
                    public DisplayListItem processToClient(Message assetMessage) {
                        return new NormalLogisticsMessageListItem(assetMessage);
                    }
                });
                mDisplayListItems.addAll(assetListItems);

                if (listener != null) {
                    listener.onSuccess(mDisplayListItems, assetMessageList.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void loadSystemMessages(final boolean isRefreshing, String tag, final OnGetFromNetListener listener) {
        if (isRefreshing) {
            mPage = 1;
        }

        final MessageRequest request = new MessageRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.MESSAGE_NEWS_LIST);
        request.setPage(mPage);
        request.setType(MessageRequest.Type.SYSTEM);
        request.setPageSize(Common.PAGER_SIZE);
        mNetBiz.sendGetRequest(request, MessageResponse.class, new NetListener<MessageResponse>() {
            @Override
            public void onSuccess(MessageResponse response) {
                mPage++;
                if (isRefreshing) {
                    mDisplayListItems.clear();
                }

                List<Message> messages = response.getData().getNewsList();
                for (int i = 0; i < messages.size(); i++) {
                    Message message = messages.get(i);

                    // 时间
                    if (i == 0 || !message.getAddTime().equals(messages.get(i - 1).getAddTime())) {
                        // 时间不同
                        MessageTimeListItem messageTimeListItem = new MessageTimeListItem(message.getAddTime());

                        // 上一页的最后一个
                        if (!isRefreshing && i == 0) {
                            DisplayListItem displayListItem = mDisplayListItems.get(mDisplayListItems.size() - 1);
                            // 上一页最后一个的时间和当前时间不同则添加
                            if (displayListItem instanceof NormalSystemMessageListItem) {
                                String lastPagerTime = ((NormalSystemMessageListItem) displayListItem).getContentData().getAddTime();
                                if (!lastPagerTime.equals(message.getAddTime())) {
                                    mDisplayListItems.add(messageTimeListItem);
                                }
                            }
                        } else {
                            mDisplayListItems.add(messageTimeListItem);
                        }
                    }
                    // 内容
                    NormalSystemMessageListItem systemMessageListItem = new NormalSystemMessageListItem(message);
                    mDisplayListItems.add(systemMessageListItem);
                }

                if (listener != null) {
                    listener.onSuccess(mDisplayListItems, messages.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void loadFavourableMessages(final boolean isRefreshing, String tag, final OnGetFromNetListener listener) {
        if (isRefreshing) {
            mPage = 1;
        }

        final MessageRequest request = new MessageRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.MESSAGE_NEWS_LIST);
        request.setPage(mPage);
        request.setType(MessageRequest.Type.FAVOURABLE);
        request.setPageSize(Common.PAGER_SIZE);
        mNetBiz.sendGetRequest(request, MessageResponse.class, new NetListener<MessageResponse>() {
            @Override
            public void onSuccess(MessageResponse response) {
                mPage++;
                if (isRefreshing) {
                    mDisplayListItems.clear();
                }

                List<Message> assetMessageList = response.getData().getNewsList();
                List<DisplayListItem> assetListItems = Functional.serverToClient(assetMessageList,
                        new Functional.Processor<Message, DisplayListItem>() {
                            @Override
                            public DisplayListItem processToClient(Message message) {
                                return new NormalFavorableMessageListItem(message);
                            }
                        });
                mDisplayListItems.addAll(assetListItems);

                if (listener != null) {
                    listener.onSuccess(mDisplayListItems, assetMessageList.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void loadDiscoveryMessages(final boolean isRefreshing, String tag, final OnGetEnrollMessagesListener listener) {
        if (isRefreshing) {
            mPage = 1;
        }

        final MessageRequest request = new MessageRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.MESSAGE_NEWS_LIST);
        request.setPage(mPage);
        request.setType(MessageRequest.Type.DISCOVERY);
        request.setPageSize(Common.PAGER_SIZE);
        mNetBiz.sendGetRequest(request, MessageResponse.class, new NetListener<MessageResponse>() {
            @Override
            public void onSuccess(MessageResponse response) {
                mPage++;
                if (isRefreshing) {
                    mObjects.clear();
                }

                List<Message> messages = response.getData().getNewsList();
                for (int i = 0; i < messages.size(); i++) {
                    Message message = messages.get(i);
                    // 时间
                    if (i == 0 || !message.getAddTime().equals(messages.get(i - 1).getAddTime())) {
                        // 时间不同
                        if (!isRefreshing && i == 0) {
                            // 上一页最后的一个时间和当前页第一个的时间相同不添加，不相同添加
                            Object lastPagerObj = mObjects.get(mObjects.size() - 1);
                            if (lastPagerObj instanceof Message) {
                                String lastPagerTime = ((Message) lastPagerObj).getAddTime();
                                if (!lastPagerTime.equals(message.getAddTime())) {
                                    mObjects.add(message.getAddTime());
                                }
                            }
                        } else {
                            mObjects.add(message.getAddTime());
                        }
                    }
                    // 内容
                    mObjects.add(message);
                }

                if (listener != null) {
                    listener.onSuccess(mObjects, messages.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void setMessageIsRead(String tag, String messageId, final OnOperateListener listener) {
        MessageReadRequest request = new MessageReadRequest();
        request.setMsgId(messageId);
        request.setTag(tag);
        request.setUrl(Urls.MEMBER_SET_NEWS_READ);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }
}
